﻿using Newtonsoft.Json;
using Robin.Logging;
using Robin.Specifications;
using Robin.Web.Mvc.Controllers;
using Smart.Project.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Smart.Project.Web.Controllers
{
    public class UserController : RobinController
    {
        IUserRepository _resp;
        IDepartmentRepository _Departmentresp;
        IRoleRepository _Roleresp;
        public UserController(IUserRepository resp, IDepartmentRepository Departmentresp, IRoleRepository Roleresp)
        {
            _resp = resp;
            _Departmentresp = Departmentresp;
            _Roleresp = Roleresp;
        }
        // GET: User
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult DetailHelp()
        {
            return View();
        }
        public ActionResult RoleHelp()
        {
            return View();
        }
        //Create
        public ActionResult Create()
        {
            MUser model = new MUser() { Id = System.Guid.NewGuid().ToString() };
            return View(model);
        }

        [HttpPost]
        public string Create(MUser model)
        {
            string result = "false";
            var dateNow = DateTime.Now;
            try
            {
                var umodel = _resp.Count(a => a.USERCODE == model.USERCODE);
                if (umodel>0)
                {
                    result = "用户账号已存在！";
                }
                else
                {
                    model.CREATETIME = decimal.Parse(dateNow.ToString("yyyyMMddHHmmss"));
                    model.UPDATETIME = decimal.Parse(dateNow.ToString("yyyyMMddHHmmss"));
                    model.DEPT = model.DEPT.Id != null ? _Departmentresp.Get(model.DEPT.Id) : null;
                    model.ROLE = model.ROLE.Id != null ? _Roleresp.Get(model.ROLE.Id) : null;
                    _resp.Insert(model);
                    result = "true";
                }
            }
            catch (Exception ex)
            {
                result = "false";
                LogManager.Logger.Error("UserController Create", ex);
            }
            return result;
        }

        //Updata
        public ActionResult Update(string Id)
        {
            MUser model = _resp.Get(Id);
            return View(model);
        }

        [HttpPost]
        public string Update(MUser model)
        {
            string result = "false";
            var dateNow = DateTime.Now;
            try
            {
                var umodel = _resp.Count(a => a.USERCODE == model.USERCODE && a.Id != model.Id);
                if (umodel >0)
                {
                    result = "用户账号已存在！";
                }
                else
                {
                    model.UPDATETIME = decimal.Parse(dateNow.ToString("yyyyMMddHHmmss"));
                    model.DEPT = model.DEPT.Id != null ? _Departmentresp.Get(model.DEPT.Id) : null;
                    model.ROLE = model.ROLE.Id != null ? _Roleresp.Get(model.ROLE.Id) : null;
                    _resp.Update(model);
                    result = "true";
                }
            }
            catch (Exception ex)
            {
                result = "false";
                LogManager.Logger.Error("UserController Update", ex);
            }
            return result;
        }

        //Detail
        public ActionResult Detail(string Id)
        {
            MUser model = _resp.Get(Id);
            return View(model);
        }

        public string Delete(string id)
        {
            string result = "false";
            try
            {
                _resp.Delete(id);
                result = "true";
            }
            catch (Exception ex)
            {
                result = "false";
                LogManager.Logger.Error("UserController Delete", ex);
            }
            return result;
        }

        public string GetUserList()
        {
            string result = string.Empty;
            string name = Request["NAME"];
            string echo = Request["sEcho"];
            string iDisplayStart = Request["iDisplayStart"];
            string iDisplayLength = Request["iDisplayLength"];
            int count = 0;
            int startIndex = 0, pagesize = 7;
            int.TryParse(iDisplayStart, out startIndex);
            int.TryParse(iDisplayLength, out pagesize);
            Expression<Func<MUser, bool>> where = item => true;
            FormatedList<List<MUser>> formlist = new FormatedList<List<MUser>>();
            where = where.And(item => item.DEPT.BMMC.Contains(name));
            var list = _resp.GetList(where, obj => obj.Asc(item => item.Id), startIndex, pagesize);
            try
            {
                count = _resp.GetAllList(where).Count();
                formlist.sEcho = int.Parse(echo);
                formlist.iTotalRecords = count;
                formlist.iTotalDisplayRecords = count;
                //foreach (var row in list)
                //{
                //    if (row.ROLE == null)
                //    {
                //        row.ROLE = new MRole();
                //        row.ROLE.BYNAME = "";
                //    }
                //}
                formlist.aaData = list.ToList();
                result = JsonConvert.SerializeObject(formlist);
            }
            catch (Exception ex)
            {
                LogManager.Logger.Error("UserController GetList", ex);
            }
            return result;
        }
    }
}